
package com.app.Cutofftime;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.app.Cutofftime.Database.DatabaseHelper;
import com.app.Cutofftime.R;
import com.google.android.material.textfield.TextInputLayout;

import java.util.regex.Pattern;

public class LoginActivity extends AppCompatActivity {

    private DatabaseHelper helper;
    private EditText password;
    private EditText user;
    private Button login;
    private TextInputLayout passwordLayout, nameLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Toolbar toolbar = findViewById(R.id.bar2);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        user = findViewById(R.id.user);
        password = findViewById(R.id.password);

        login = findViewById(R.id.login);
        helper = new DatabaseHelper(getApplicationContext());

        nameLayout = findViewById(R.id.nameLayout);
        passwordLayout = findViewById(R.id.passwordLayout);

        nameLayout.requestFocus();

        login.setOnClickListener(v -> {
            String user_email = user.getText().toString().trim();
            String password2 = password.getText().toString();

            if (user.length() == 0) {
                showError(nameLayout, "User cannot be empty");
                passwordLayout.setErrorEnabled(false);
            } else if (password.length() == 0) {
                showError(passwordLayout, "Password cannot be empty");
                nameLayout.setErrorEnabled(false);

            }
            else if (helper.checkLogin(user_email, password2)) {
                nameLayout.setErrorEnabled(false);
                passwordLayout.setErrorEnabled(false);
                Toast.makeText(LoginActivity.this, "Log in successfully", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(getApplicationContext(), DashboardActivity.class);


                startActivity(i);
            } else if (!helper.checkUser(user_email)) {
                showError(nameLayout, "Incorrect user!");
                passwordLayout.setErrorEnabled(false);
            } else {
                showError(passwordLayout, "Incorrect password!");
                nameLayout.setErrorEnabled(false);
            }
        });

        password.setOnClickListener(v -> {
            passwordLayout.setErrorEnabled(false);
        });
    }

    public void showError(TextInputLayout view, String errorMsg) {
        view.setError(errorMsg);
        view.requestFocus();
    }

    @Override
    protected void onRestart() {
        user.setText("");
        password.setText("");
        nameLayout.requestFocus();
        super.onRestart();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        finish();
        return true;
    }
}

